<?php
require_once './application/core/CoreBase.php';
require_once './library/XML/xmlToObjArray.php';
require_once './application/models/Tables/cache.php';
require_once './application/core/Client/visualisationSoap.php';
require_once './application/entity/Visualisation/valideIp.php';
require_once './application/entity/Visualisation/rechercheVisualisation.php';

class valideIpCore extends IndexCore
{
    public function valideIp()
    {
        $id = uniqid();
        $ip = $this->_controller->getRequest()->getParam('ip');
        $mask = $this->_controller->getRequest()->getParam('mask');
        if ( preg_match( '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $ip ) == 0 || preg_match( '/^[0-9]{1,5}$/', $mask ) == 0 )
        {
            $this->valideIpErreurIpMask();
            return false;
        }
        $params = array( 'Ip'=> $ip,
                'Mask'=> $mask );
        $c = new visualisationSoap( $this->_controller );
        $c->valideIp( $params, 'valideIpResult' );
        $this->MiseEnAttenteReponse(true, '/visualisation/valideipresult', null);
    }

    private function valideIpErreurIpMask()
    {
        $result = new ValideIp();
        $result->valide = "error";
        $this->MiseEnAttenteReponse( false, '/visualisation/valideipresult', $result);
        return;
    }

    public function valideIpResult()
    {
        $cache = new cache();
        $rst = $cache->getInfo( $this->_controller->_idClient, 'valideIpResult' );
        if ( $rst != false )
        {
            $val = $this->parseValideIpResp( $rst[0]['Xml'] );
            $result = new ValideIp();
            $result->valide = false;
            if ( $val[0] == true )
            {
                $result->valide = $val[0];
                $result->ip = $val[1];
                $result->mask = $val[2];
            }
            $cache->deleteInfo( $this->_controller->_idClient, 'valideIpResult', $rst[0]['Id'] );
            $this->MiseEnAttenteReponse( false, '/visualisation/valideipresult', $result );
        }
        else
            $this->MiseEnAttenteReponse( true, '/visualisation/valideipresult', null );
    }

    private function parseValideIpResp( $xml  )
    {
        $obj = new xmlToObjArray();
        $xml = $obj->parse_xml_data( $xml, false );
        $values = array( $xml[0]['PARAM'][0]['VALIDE'][0][0],
                $xml[0]['PARAM'][0]['IP'][0]['IP'][0][0],
                $xml[0]['PARAM'][0]['IP'][0]['MASK'][0][0] );
        return $values;
    }
}
?>
